package com.tingyun.sign.admin.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.tingyun.sign.admin.bean.ScUManagerDto;
import com.tingyun.sign.admin.service.ManagerService;
import com.tingyun.sign.core.entity.ScUManagerBean;
import com.tingyun.sign.utils.BaseController;
import com.tingyun.sign.utils.Page;

@Controller
@RequestMapping("manager")
public class ManagerController extends BaseController {

	@Autowired
	private ManagerService managerService;

	/**
	 * 检查用户登录 0:用户名不存在，1：密码错误，2：校验通过
	 */
	@RequestMapping("login")
	@ResponseBody
	public Map<String, Object> login(ScUManagerBean bean, HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		Integer result = null;
		try {
			result = managerService.login(bean);
			if (result == 2) {
				bean = managerService.findByNameAndPass(bean.getManagerName(),
						bean.getPassWord());
				session.removeAttribute(USER);
				setSessionUser(session, bean);
			}
			map.put("result", result);
		} catch (Exception e) {
			map.put("info", e.getMessage());
		}
		return map;
	}

	@RequestMapping("logout")
	public String logout(HttpSession session) {
		if (null != session.getAttribute(USER)) {
			session.removeAttribute(USER);
		}
		return "redirect:../view/login";
	}

	/**
	 * 添加管理员
	 * 
	 * @param bean
	 * @param session
	 * @return
	 */
	@RequestMapping("add")
	@ResponseBody
	public Map<String, Object> addManager(ScUManagerBean bean,
			HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		Integer result = null;
		try {
			ScUManagerBean curUser = getSessionUser(session);
			if (managerService.findByName(bean.getManagerName())) {
				map.put("info", "用户已经存在,请重新创建");
			} else if (curUser == null || !curUser.getType().equals(0)) {
				map.put("info", "无新增权限，请登录超级管理员账号");
			} else {
				if (bean.getType().equals(0)
						&& managerService.checkByType() == 0) {
					map.put("info", "已经存在超级管理员账号，请创建非管理员账号");
				} else {
					bean.setCuser(curUser.getId());
					bean.setMuser(curUser.getId());
					bean.setCtime(new Date());
					bean.setMtime(new Date());
					result = managerService.addManager(bean);
					map.put("result", result);
				}
			}
		} catch (Exception e) {
			map.put("info", e.getMessage());
		}
		return map;
	}

	/**
	 * 编辑管理员信息
	 * 
	 * @param bean
	 * @param session
	 * @return
	 */
	@RequestMapping("edit")
	@ResponseBody
	public Map<String, Object> editManager(ScUManagerDto bean,
			HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		ScUManagerBean newBean = null;
		try {
			ScUManagerBean curUser = getSessionUser(session);
			bean.setMuser(curUser.getId());
			newBean = managerService.editManager(bean);
			//session.removeAttribute(USER);
			//setSessionUser(session, newBean);
			map.put("result", 1);
		} catch (Exception e) {
			map.put("info", e.getMessage());
		}
		return map;
	}

	@RequestMapping(value = "list")
	public ModelAndView list(ScUManagerBean bean) {
			ModelAndView mv = new ModelAndView();
			mv.setViewName("manager/list");
			mv.addObject("obj", bean);
			return mv;
	}

	@RequestMapping(value = "delete")
	@ResponseBody
	public Map<String, Object> delete(HttpSession session, ScUManagerBean bean) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			ScUManagerBean manager = getSessionUser(session);
			System.out.println(bean.getId());
			bean.setMuser(manager.getId());
			Integer result = managerService.deleteById(bean);
			map.put("result", result);
		} catch (Exception e) {
			map.put("info", e.getMessage());
		}
		return map;
	}

	@RequestMapping(value = "listPage")
	@ResponseBody
	public Map<String, Object> findPage(HttpSession session, ScUManagerBean bean) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			ScUManagerBean manager = getSessionUser(session);
			bean.setType(manager.getType());
			Page<ScUManagerBean> page = managerService.findAllManageres();
			map.put("total", page.getTotalCount());
			map.put("rows", page.getResultList());
		} catch (Exception e) {
			map.put("info", e.getMessage());
		}
		return map;
	}

	@RequestMapping(value = "tree")
	@ResponseBody
	public String tree(ScUManagerBean bean,HttpSession session) {
		ScUManagerBean curUser = getSessionUser(session);
		String adminTreeStr = "[{text : \"签到系统\",iconCls:\"icon-home\","
				+ "children : [ "
				+ "{text : \"活动管理\",iconCls:\"icon-activity\",attributes : {url : \"../activity/list\"}},"
				+ "{text : \"客户管理\",iconCls:\"icon-custom\",attributes : { url : \"../companyUser/list\"}},"
				+ "{text : \"问题管理\",iconCls:\"icon-question\",attributes : {url : \"../questions/index\"}},"
				+ "{text : \"管理员管理\",iconCls:\"icon-admin\",attributes : {url : \"../manager/list\"}},"
				+ "{text : \"活动图片管理\",iconCls:\"icon-pic\",attributes : {url : \"../pics/list\"}}"
				+ "]}]";
		String treeStr = "[{text : \"签到系统\",iconCls:\"icon-home\","
				+ "children : [ "
				+ "{text : \"活动管理\",iconCls:\"icon-activity\",attributes : {url : \"../activity/list\"}},"
				+ "{text : \"客户管理\",iconCls:\"icon-custom\",attributes : { url : \"../companyUser/list\"}},"
				+ "{text : \"问题管理\",iconCls:\"icon-question\",attributes : {url : \"../questions/index\"}},"
				+ "{text : \"活动图片管理\",iconCls:\"icon-pic\",attributes : {url : \"../pics/list\"}}"
				+ "]}]";
		if (curUser == null || !curUser.getType().equals(0)) {
			return treeStr;
		} else {
			return adminTreeStr;
		}
		
	}
}
